package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OmsOrderLink;
import com.settlement.system.mapper.OmsOrderLinkMapper;
import com.settlement.system.service.OmsOrderLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OmsOrderLinkQuery;
import com.settlement.system.model.form.OmsOrderLinkForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.OmsOrderLinkImportVo;

/**
 * <p>
 * OMS销售订单对应表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-14
 */
@Service
public class OmsOrderLinkServiceImpl extends ServiceImpl<OmsOrderLinkMapper, OmsOrderLink> implements OmsOrderLinkService {

    @Override
    public Page<OmsOrderLink> getPage(OmsOrderLinkQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OmsOrderLink> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOmsOrderLink(OmsOrderLinkForm omsOrderLinkForm) {
         OmsOrderLink omsOrderLink = new OmsOrderLink();
         BeanUtil.copyProperties(omsOrderLinkForm, omsOrderLink);
         omsOrderLink.setCreateTime(null);
         return this.saveOrUpdate(omsOrderLink);
     }

     @Override
     public OmsOrderLinkForm getOmsOrderLinkFormData(Long id) {
         OmsOrderLink omsOrderLink = this.getById(id);
         OmsOrderLinkForm omsOrderLinkForm = new OmsOrderLinkForm();
         BeanUtil.copyProperties(omsOrderLink, omsOrderLinkForm);
         return omsOrderLinkForm;
     }

     @Override
     public boolean deleteOmsOrderLink(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OmsOrderLinkImportVo> voList)  {
        List<OmsOrderLink> omsOrderLinkList = new ArrayList<>();
        for (OmsOrderLinkImportVo importVo : voList) {
            OmsOrderLink omsOrderLink = new OmsOrderLink();
            BeanUtil.copyProperties(importVo, omsOrderLink);
            omsOrderLinkList.add(omsOrderLink);
        }
        this.saveOrUpdateBatch(omsOrderLinkList);
    }

    @Override
    public List<OmsOrderLinkImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OmsOrderLink> omsOrderLinkList = this.getBaseMapper().selectBatchIds(ids);
        List<OmsOrderLinkImportVo> voList = new ArrayList<>();
        for (OmsOrderLink omsOrderLink : omsOrderLinkList) {
            OmsOrderLinkImportVo vo = new OmsOrderLinkImportVo();
            BeanUtil.copyProperties(omsOrderLink, vo);
            voList.add(vo);
        }
        return voList;
    }
}
